<template>
  <div class="potentialCustomers">
    <page-header :title="'实习计划管理'"></page-header>
    <div class="search_area">
      <el-row>
        <el-form :form="queryForm" label-width="1rem">
          <el-col :span="6">
            <el-form-item label="实习方式">
              <el-select v-model="queryForm.internshipWay" clearable placeholder="请选择实习方式" style="width:100%">
                <el-option v-for="item in InternshipWayList" :label="item.described" :value="item.code" :key="item.code"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="发布日期">
              <el-date-picker
                style="width: 100%"
                v-model="queryForm.createTime"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="计划标题">
              <el-input type="text" v-model="queryForm.projectHeading"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6" v-if="permission == '0'">
            <el-form-item label="指导教师">
              <el-select v-model="queryForm.teacherId" placeholder="请选择指导教师" style="width:100%">
                <el-option v-for="item in teacherList" :label="item.userName" :key="item.uuid" :value="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
        <el-col :span="permission == '0' ? 24 : 6">
          <div class="btn_area">
            <el-button class="confrim" @click="btnClick">查询</el-button>
            <el-button class="cancle" @click="reset">重置</el-button>
            <el-button class="confrim" @click="add">新增</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <table-list :fields="fields" :list="list" :pagination="pagination" :total="total" @sizeChange="handleSizeChange" @currentChange="handleCurrentChange" @handle="onHandle"></table-list>
    <el-dialog :title="code == '1' ? '新增实习计划': '编辑实习计划'" :visible.sync="visible" @close="close">
      <el-row>
        <el-form :form="planForm" label-width="1rem">
          <el-col :span="11">
            <el-form-item label="计划标题" required>
              <el-input type="text" v-model="planForm.projectHeading" placeholder="请填写实习计划标题"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="实习目的" required>
              <el-input type="text" v-model="planForm.internshipPurpose" placeholder="请填写实习目的"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="实习方式" required>
              <el-select v-model="planForm.internshipWay" clearable placeholder="请选择实习方式" style="width:100%">
                <el-option v-for="item in InternshipWayList" :label="item.described" :value="item.code" :key="item.code"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11" v-if="permission == '0'">
            <el-form-item label="指导教师" required>
              <el-select v-model="planForm.teacherId" placeholder="请选择指导教师" style="width:100%">
                <el-option v-for="item in teacherList" :label="item.userName" :key="item.uuid" :value="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="发布日期" required>
              <el-input type="text" v-model="planForm.createTime" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <el-divider v-if="code !== '1'"></el-divider>
      <div v-if="code !== '1'">
        <div style="margin-bottom: 0.2rem;">
          <span style="font-size: 0.16rem">实习岗位列表</span>
        </div>
        <table-list :fields="fields2" :list="postList" :page="false" :pagination="{currentChange: 1,pageSize:100}"></table-list>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancle" @click="visible = false">取 消</el-button>
        <el-button class="confrim" @click="confrim">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import tableList from '@/components/table.vue'
import pageHeader from '@/components/pageHeader.vue'
export default {
  name: 'potentialCustomers',
  components: {
    tableList,
    pageHeader
  },
  data() {
    return {
      planForm: {},
      queryForm: {
        internshipWay: '',
        projectHeading: '',
        createTime: '',
        teacherId: ''
      },
      list: [],
      fields: [
        {
          prop: 'rowNum',
          label: '序号',
          align: 'center'
        },
        {
          prop: 'projectHeading',
          label: '实习计划标题',
          align: 'center'
        },
        {
          prop: 'internshipWayName',
          label: '实习方式',
          align: 'center'
        },
        {
          prop: 'teacherName',
          label: '指导教师',
          align: 'center'
        },
        {
          prop: 'createTime',
          label: '发布日期',
          align: 'center'
        },
        {
          prop: 'oper',
          label: '操作',
          align: 'center',
          btns: [
            {type: 'update',desc:'编辑'}
          ]
        }
      ],
      pagination: {
        currentPage: 1,
        pageSize: 10
      },
      total: 0,
      code: '',
      visible: false,
      permission: '',
      teacherList: [],
      InternshipWayList: [],
      fields2: [
        {
          prop: 'postName',
          label: '岗位名称',
          align: 'center'
        },
        {
          prop: 'applicationDeadline',
          label: '报名截止时间',
          align: 'center'
        },
        {
          prop: 'createTime',
          label: '发布日期',
          align: 'center'
        },
        {
          prop: 'endDate',
          label: '岗位结束日期',
          align: 'center'
        }
      ],
      postList: []
    }
  },
  methods: {
    close() {
      this.planForm = {};
    },
    confrim() {
      let url = this.code == '1' ? this.$api.plan.create : this.$api.plan.update
      if (this.permission == '1') {
        this.planForm.teacherId = JSON.parse(sessionStorage.getItem('user')).uuid;
      }
      console.log(this.planForm);
      this.$post(url,this.reqData(this.planForm)).then(res => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message);
          this.visible = false;
          this.queryList()
        } else {
          this.$message.error(res.data.message);
        }
      })
    },
    onHandle(data) {
      let {row,type} = data;
      console.log(data);
      this.code = '2';
      let url = this.$api.plan.info;
      this.$post(url,this.reqData(row.uuid)).then(res => {
        if (res.data.code === 200) {
          console.log(res.data.data);
          let data = res.data.data;
          this.planForm = data;
          this.postList = data.postList
          this.visible = true;
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    reset() {
      this.queryForm = {
        name: '',
        area: '',
        type: ''
      }
    },
    add() {
      this.visible = true,
      this.code = '1';
      this.planForm.createTime = this.$time()
    },
    btnClick() {
      this.queryList();
    },
    queryList() {
      let url = this.$api.plan.list;
      this.queryForm.adminId = JSON.parse(sessionStorage.getItem('user')).uuid
      this.$post(url,this.reqData(this.queryForm,this.pagination.currentPage,this.pagination.pageSize)).then(res => {
        if (res.data.code == 200) {
          let data = res.data.data
          this.list = data.list;
          this.list.forEach((item,index) => {
            item.rowNum = (this.pagination.currentPage - 1) * this.pagination.pageSize + index + 1
          })
          this.total = data.total;
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    handleSizeChange(val) {
      console.log(val);
      this.pagination.pageSize = val;
      this.queryList();
    },
    handleCurrentChange(val) {
      console.log(val);
      this.pagination.currentPage = val;
      this.queryList();
    },
    queryTeacherList() {
      let url = this.$api.user.list;
      this.$post(url,this.reqData({},this.pagination.currentPage,this.pagination.pageSize)).then(res => {
        if (res.data.code == 200) {
          this.teacherList = res.data.data.filter(item => item.permission == '1');
          console.log(this.teacherList);
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    queryInternshipWay() {
      this.$post(this.$api.common.code,this.reqData('Internship_Way')).then(res => {
        if (res.data.code === 200) {
          this.InternshipWayList = res.data.data;
        }
      })
    },
  },
  mounted() {
    this.permission = JSON.parse(sessionStorage.getItem('user')).permission
    if (this.permission == '0') {
      this.queryTeacherList();
    }
    this.queryInternshipWay();
    this.queryList();
  }
}
</script>
<style scoped>
.potentialCustomers {
  background: #fff;
  overflow: hidden;
  padding-bottom: 0.15rem;
}
</style>